package com.jic.finance.base;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ZW on 2020/1/9 11:31
 * 通用日期处理类
 * 注意：这里日期处理大量使用 SimpleDateFormat，多线程情况下可能会有问题，用到的人自行修改成 LocalDate
 */
public class JicDateHelper {

    /**
     * 年-月-日 时:分:秒
     */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     *周，月，日，时间，年
     */
    public static final String DATETIME_ENGLISH_FORMAT = "EEE MMM dd HH:mm:ss z yyyy";
    /**
     * 年-月-日
     */
    public static final String DATE_FORMAT = "yyyy-MM-dd";

    /**
     * 年月
     */
    public static final String DATE_FORMAT_SIMPLE = "yyyyMM";
    /**
     * 年月
     */
    public static final String DATE_FORMAT_YEAR_MONTH = "yyyy-MM";

    /**
     * 年月日
     */
    public static final String DATE_FORMAT_DAY = "yyyyMMdd";
    /**
     * 年月周
     */
    public static final String DATE_FORMAT_WEEK = "yyyyMM-EEE";

    /**
     * 年月周数
     */
    public static final String DATE_FORMAT_WEEKNUM = "yyyyMM--WW";

    /**
     * 年月
     */
    public static final String DATE_FORMAT_MONTH = "yyyyMM";
    /**
     * 年月季度
     */
    public static final String DATE_FORMAT_QUARTER = "yyyy-";


    /**
     * 时间开始
     */
    public static final String TIME_BEGIN = "00:00:00";

    /**
     * 时间结束
     */
    public static final String TIME_END = "23:59:59";

    /**
     * 格式化日期
     * @param date
     * @param pattern
     * @return
     */
    public static String dateFormatter(Date date, String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 查询今天开始时间
     * @return
     */
    public static String getTodayBeginTime(){
        Calendar calendar = Calendar.getInstance();
        String dayFirst = dateFormatter(calendar.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(dayFirst).append(" ").append(TIME_BEGIN);
        return str.toString();
    }

    /**
     * 查询今天结束时间
     * @return
     */
    public static String getTodayEndTime(){
        Calendar calendar = Calendar.getInstance();
        String dayFirst = dateFormatter(calendar.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(dayFirst).append(" ").append(TIME_END);
        return str.toString();
    }

    /**
     * 本星期第一天
     * @return
     */
    public static String getThisWeekFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        String dayFirst = dateFormatter(calendar.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(dayFirst).append(" ").append(TIME_BEGIN);
        return str.toString();
    }


    /**
     * 本星期最后一天
     * @return
     */
    public static String getThisWeekLastDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.DAY_OF_WEEK);
        String dayFirst = dateFormatter(calendar.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(dayFirst).append(" ").append(TIME_BEGIN);
        return str.toString();
    }

    /**
     * 当月第一天
     * @return
     */
    public static String getThisMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);

        String dayFirst = dateFormatter(gcLast.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(dayFirst).append(" ").append(TIME_BEGIN);
        return str.toString();
    }

    /**
     * 当月最后一天
     * @return
     */
    public static String getThisMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String s = dateFormatter(calendar.getTime(), DATE_FORMAT);
        StringBuffer str = new StringBuffer().append(s).append(" ").append(TIME_END);
        return str.toString();

    }

    /**
     * 查询这个月所有天数
     * @return
     */
    public static List<String> getDaysOfThisMonth(){
        List<String> days = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DATE, 1);

        int month = cal.get(Calendar.MONTH);
        while(cal.get(Calendar.MONTH) == month){
            days.add(dateFormatter(cal.getTime(), DATE_FORMAT));
            cal.add(Calendar.DATE, 1);
        }
        return days;
    }

    /**
     * 查询今年所有月数
     * @return
     */
    public static List<String> getMonthsOfThisYear(){
        List<String> months = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, 0);

        int year = cal.get(Calendar.YEAR);
        while(cal.get(Calendar.YEAR) == year){
            months.add(df.format(cal.getTime()));
            cal.add(Calendar.MONTH, 1);
        }
        return months;
    }


    /**
     * 日期时间字符串转秒
     * @param datetime
     * @return
     */
    public static Long stringToSecond(String datetime){
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);
        try {
            Date parse = sdf.parse(datetime);
            return  parse.getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取今年的第一天
     * @return
     */
    public static Date getCurrYearFirst(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取今年的最后一天
     * @return
     */
    public static Date getCurrYearLast(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }


    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 日期追加天数
     * @param day 添加的天数
     * @return
     */
    public static Date afterDate(Date base,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(base);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 减去秒
     * @param second 秒
     * @return
     */
    public static Date subtractSecond(Date base,int second){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(base);
        calendar.add(Calendar.SECOND,0-second);
        return calendar.getTime();
    }

    /**
     * 时间字符串转日期
     * @param date
     * @param format
     * @return
     */
    public static Date formatDate(String date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询两个日期间的天数
     * @return
     */
    public static List<String> getDaysBetweenTwoDate(Date startTime, Date endTime) {
        List<String> days = new ArrayList<>();
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startTime);

        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endTime);

        while (startCal.getTimeInMillis() <= endCal.getTimeInMillis()) {
            days.add(dateFormatter(startCal.getTime(), DATE_FORMAT));
            startCal.add(Calendar.DATE, 1);
        }
        return days;
    }

    public static Date formatDateEnglish(String date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.ENGLISH);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 当前季度
     * @return
     */
    public static int getCurQuarter(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        int currentMonth = calendar.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3){
                return 1;
            }else if (currentMonth >= 4 && currentMonth <= 6){
                return 2;
            } else if (currentMonth >= 7 && currentMonth <= 9){
                return 3;
            }else if (currentMonth >= 10 && currentMonth <= 12){
                return 4;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 本季度开始时间
     * @return
     */
    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 6);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间。即2012-03-31 23:59:59
     *
     * @return
     */
    public static Date getCurrentQuarterEndTime() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getCurrentQuarterStartTime());
        cal.add(Calendar.MONTH, 3);
        return cal.getTime();
    }

}
